//
//  FavoriteViewController.m
//  SZBus3
//
//  Created by Kengi Chan on 10/22/09.
//  Copyright 2009 IDMT. All rights reserved.
//

#import "FavoriteViewController.h"
#import "RouteDetailViewController.h"
#import "StationDetailViewController.h"

//#import "ChildViewController.h"

@implementation FavoriteViewController

//@synthesize table;
@synthesize listFavorite;
@synthesize listFavoriteType;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
	
	
	self.title = @"Favorite";
	NSString *path = [[NSBundle mainBundle] pathForResource:@"favorite"	ofType:@"plist"];
	NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
	self.listFavorite = dict;
	[dict release];
	
	//NSArray *array = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
	self.listFavoriteType = [NSArray arrayWithObjects: @"Route", @"Station", nil]; //[[NSArray alloc] initWithArray:array copyItems:YES]  ;
//	[array release];
	[super viewDidLoad];
//	
    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    //self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.listFavorite =nil;
	self.listFavoriteType =nil;
}


- (void)dealloc {
//	[table release];
	[listFavorite release];
	[listFavoriteType release];
	[super dealloc];
}


#pragma mark Table view methods
- (CGFloat)tableView:(UITableView *)tableView 
	heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 36.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return ([listFavoriteType count] > 0) ? [listFavoriteType count] : 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView 
	numberOfRowsInSection:(NSInteger)section 
{
	if([listFavoriteType count] == 0)
		return 0;
    NSString *favoriteType = [listFavoriteType objectAtIndex:section];
	NSArray *favoriteSection = [listFavorite objectForKey:favoriteType];
	return [favoriteSection count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
	cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	
	NSString *favoriteType = [listFavoriteType objectAtIndex:section];
	NSArray *favoriteSection = [listFavorite objectForKey:favoriteType];
	
	static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
#ifdef __IPHONE_3_0
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: CellIdentifier] autorelease];
#else
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
									   reuseIdentifier:CellIdentifier] autorelease];
#endif
    }
	cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	UIImage *image = (section == 0) ? [UIImage imageNamed:@"imgroute.png"] :[UIImage imageNamed:@"imgstation.png"];	
	
#ifdef __IPHONE_3_0
	cell.imageView.image = image;
    cell.textLabel.text = [favoriteSection objectAtIndex:row];	
	cell.textLabel.font = [UIFont systemFontOfSize:18.0 ];
	//cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
#else	
	cell.image = image;
    cell.text = [favoriteSection objectAtIndex:row];
	cell.font = [UIFont boldSystemFontOfSize:18.0];	
#endif
	
    return cell;
}
	
-(NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
	if( [listFavoriteType count] == 0)
		return @"";
	
	NSString *favoriteType = [listFavoriteType objectAtIndex:section];
	return	favoriteType;
	
}


-(NSIndexPath *)tableView:(UITableView *)tableView 
 willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSUInteger row = [indexPath row];
//    if (row == 0)
//        return nil;
//    
    return indexPath; 
}

- (void)tableView:(UITableView *)tableView 
	didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	NSUInteger section = [indexPath section];
	NSUInteger row = [indexPath row];
	NSString *favoriteType = [listFavoriteType objectAtIndex:section];
	NSArray *favoriteSection = [listFavorite objectForKey:favoriteType];
	NSString *favorite = [favoriteSection objectAtIndex:row];
	
	if ([indexPath section] == 0) {
		RouteDetailViewController *nextController = [[RouteDetailViewController alloc]
							  initWithNibName: @"RouteDetailView" bundle:nil];
		nextController.routeName = favorite;
		[self.navigationController pushViewController:nextController animated:YES];
	}else{
		StationDetailViewController *nextController = [[StationDetailViewController alloc]
							  initWithNibName: @"StationDetailView" bundle:nil];
		nextController.stationName = favorite;
		[self.navigationController pushViewController:nextController animated:YES];
	}	
    [favoriteSection release];
	[favoriteType release];
	[favorite release];
    

}

//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//	return listFavoriteType;
//}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/




@end

